<script setup>
import { computed, ref, watch } from 'vue'
import { useTableList } from '@/hooks/useTableList'

const modelValue = defineModel('visible', {
  type: Boolean,
  required: true,
})

const props = defineProps({
  title: {
    type: String,
    default: () => '选择巡检点',
  },
})

const tableRef = ref()

const searchParams = ref({
  name: '',
  position: '',
})

const selectedData = ref([])

const {
  Search,
  toggleSearchStatus,
  showToggle,
  handleToggleSearch,
  tableData,
  paginationParams,
  total,
  initTableList,
  handleSizeChange,
  handleReset,
  handleDelete,
  addRef,
  detailRef,
  currentDetail,
  handleAdd,
  handleEdit,
  handleDetail,
  handleClose,
  searchFromRef,
} = useTableList(searchParams)

const onSelectionChange = (selection) => {
  console.log('selection:', selection)
}

const cancel = () => {
  modelValue.value = false
}

const handleSubmit = () => {
  console.log('提交')
  // 手动设置el-table选中项
  tableRef.value?.toggleRowSelection(tableData.value[3], true)
}

const visible = computed({
  get: () => modelValue.value,
  set: (val) => {
    modelValue.value = val
  },
})

watch(
  () => modelValue.value,
  (value) => {
    console.log('modelValue change:', value)
    if (value) {
      for (let i = 0; i < 10; i++) {
        tableData.value.push({
          pkid: i.toString(),
          code: 'aaaaa',
          name: '巡检点' + i,
          type: '消防设施类',
          address: '1栋负1层东南处1号点位',
          park: '园区一',
        })
      }
    } else {
      searchParams.value.name = ''
      searchParams.value.position = ''
      paginationParams.value.pageIndex = 1
      tableData.value = []
      selectedData.value = []
    }
  },
)
</script>

<template>
  <el-dialog
    class="custom-dialog"
    v-model="visible"
    align-center
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    overflow
    :title="title"
    width="800px"
  >
    <div class="custom-table-list-wrapper">
      <div class="search" :class="{ active: toggleSearchStatus }">
        <div class="search-left" ref="searchFromRef">
          <el-form class="search-form" inline>
            <el-form-item label="巡检点名称">
              <el-input
                class="search-input"
                v-model="searchParams.name"
                placeholder="请输入"
                clearable
              ></el-input>
            </el-form-item>
            <el-form-item label="位置">
              <el-input
                class="search-input"
                v-model="searchParams.position"
                placeholder="请输入"
                clearable
              ></el-input>
            </el-form-item>
          </el-form>
          <a
            class="search-left-toggle"
            v-if="showToggle"
            @click="handleToggleSearch"
          >
            <svg-icon :name="toggleSearchStatus ? 'up' : 'down'" />
            {{ toggleSearchStatus ? '收起' : '展开' }}
          </a>
        </div>
        <div class="search-right">
          <el-button type="primary" @click="initTableList(1)"
            ><svg-icon name="search" size="14px" />搜索</el-button
          >
          <el-button @click="handleReset()"
            ><svg-icon name="reset" size="17px" />重置</el-button
          >
        </div>
      </div>

      <div class="content">
        <div class="content-table">
          <el-table
            class="custom-table"
            ref="tableRef"
            :data="tableData"
            @selection-change="onSelectionChange"
          >
            <el-table-column type="selection" width="55" />
            <el-table-column type="index" label="序号" width="60px" />
            <el-table-column prop="code" label="巡检点编号" />
            <el-table-column prop="name" label="巡检点名称" />
            <el-table-column prop="type" label="巡检点类型" />
            <el-table-column label="地理位置">
              <template #default="scope">
                <el-row align="middle">
                  <span>{{ scope.row.address }}</span>
                  <SvgIcon name="location1" size="14px" />
                </el-row>
              </template>
            </el-table-column>
            <el-table-column prop="park" label="所属园区" />
          </el-table>
        </div>
        <div class="content-page">
          <el-pagination
            class="custom-pagination"
            :current-page="paginationParams.pageIndex"
            :page-size="paginationParams.pageSize"
            background
            :page-sizes="[10, 20, 30, 50]"
            layout="total, prev, pager, next, sizes, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="initTableList"
          />
        </div>
      </div>
    </div>

    <template #footer>
      <span class="selected-count">已选择{{ selectedData.length }}条</span>
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped src="@/assets/styles/table.scss"></style>
<style lang="scss" scoped>
.custom-table-list-wrapper {
  .search {
    margin-bottom: 0;
    padding: 0;
    box-shadow: none;

    &-input {
      width: 180px;
    }
  }

  .content {
    padding: 0;
  }
}
</style>
